GBML manual / Collision

Tile flags

Give your own names to kinds of tile, like diggable or hurts. Paint them in the Room Editor and check for them in your code, using the same name in both places.

The idea

Some tiles in your game are special. A patch of dirt can be dug up. A puddle makes the player slip. A spike hurts. The game already knows whether a tile blocks you: it is solid, or it is open floor, or it is low cover. It has no idea what you mean by "dirt".

A tile flag is how you tell it. You pick a name, like diggable. You paint that flag onto the tiles that should have it. Then your code asks, does the tile under me have the diggable flag?

A tile can have several flags at once. A tile can also be solid and diggable. Whether a tile blocks and which flags it has are two separate things.

Name a flag

  1. Open the Room Editor and find Collision in the left panel.
  2. Press Edit Tile Flags.
  3. Type a name in one of the six boxes. diggable works.
  4. Press Save.

You can name up to six flags in a game. Each one is a slot, and the slot is what gives the flag its number. The first box is the number 4, the next is 8, then 16, 32, 64 and 128. Clearing a name frees its slot. It never moves the other flags.

Names can have letters, numbers, spaces, - and _. Under each name the dialog shows the word your code will use, or tells you what is wrong with the name.

Paint a flag

Press the flag in the Collision panel so it looks pressed, turn on Attribute brush (the button at the top), and paint. Painting sets the flag on the tiles you touch. Erasing clears it. Only the flags you pressed change. Every other flag on those tiles stays exactly as it was.

Pick Keep (the fourth button after Pass, Solid and Low cover) if you want to paint a flag without changing which tiles are solid.

Use a flag in your code

Every flag you name is also a word in your code. The word is FLAG_ and then the name in capital letters. A flag called diggable is the word FLAG_DIGGABLE. A name with a space, like hard rock, becomes FLAG_HARD_ROCK.

You do not have to set anything else up. Here the walker digs the dirt in front of it:

// Dig the tile ahead, but only if it has the diggable flag.
var ahead_x = x + 8;
if ((tile_flags(ahead_x, y) & FLAG_DIGGABLE) != 0) {
    tile_set_class(ahead_x, y, TILE_PASS);
}

tile_flags gives back the tile's whole number. The & keeps only the part you ask about. If that part is not 0, the tile has the flag.

To turn a flag on or off, use tile_set_flags:

// Make the tile under the walker wet, and leave its other flags alone.
var flags = tile_flags(x, y);
tile_set_flags(x, y, flags | FLAG_WET);

The word is only a number in disguise. The build swaps it for the real number before anything runs, so the game never sees the name. Nothing is looked up while you play, and it costs nothing. You cannot put a new value into a word, because it is a number and not a variable.

Names that cannot work

Your code has to be able to say the word, so a flag name is checked like any other name. A name cannot have a character that is not a letter, a number, a space, - or _. Two flags cannot make the same word: Hard Rock and hard_rock are both FLAG_HARD_ROCK, and the build stops and names both. A word also cannot be one that something else already has, like an object, a script or a #macro.

When a flag's name cannot work, that flag gets no word at all. That way a word never means the wrong flag. The build tells you what to change, and so does the Edit Tile Flags dialog, before you save.

If your game already has its own #macro FLAG_DIGGABLE 4 and you then name a flag diggable, the macro keeps the word and the build stops with a message about it. Delete the macro line. The flag already gives you the word, with the right number.

If you write a FLAG_ word that is not there, the message says so. It lists the flags you do have, or tells you how to make the first one.

Flags your code uses show up in the editor

The other way round works too. If your code uses a flag, the Room Editor shows it, so you never use a flag the editor does not know about.

Under the Edit Tile Flags button, press the small ? next to Tile flags in your code. The Collision panel lists every flag your code uses:

Click a place in the list to open it in the code.

The editor can only read a number that is written out. If the number is made while the game runs, like tile_flags(x, y) & mask where mask is a variable, the panel says there are places it cannot read.

One list

The names live in one list, in the file tileFlags.json in your project's folder. The Collision panel edits that list. The build reads it. When you press Name it, that writes into the same list. Nothing keeps a second copy, so the name you paint with and the name your code uses can never drift apart.

You can also edit tileFlags.json by hand. It looks like this, with slot 1 first:

{ "flags": ["diggable", "", "", "wet", "", ""] }

Limits

See also: tile_flagstile_set_flagstile_classtile_solidCollision layersProject settings